.import RadioGlobal 1.0  as RadioGlobal

/**
    生成随机整数
**/
function getRandomIntInclusive(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min; // 加 1 以包含 max
}

/**
* paly : 播放频道
* freq : 播放的频道
**/
function playChannel(freq) {
    console.log( "开始播放频道：" , freq )
}


/**
* stop : 停止播放频道
**/
function stopChannel() {
    console.log( "停止频道" )
}


/**
* search : 搜索频道
**/
function searchChannel() {
    console.log( "搜索频道" )
    RadioGlobal.RadioData.searchChannelData.clear()

    RadioGlobal.RadioData.searchChannelData.append({freq:getRandomIntInclusive(9000 , 20000)})
    RadioGlobal.RadioData.searchChannelData.append({freq:getRandomIntInclusive(9000 , 20000)})
    RadioGlobal.RadioData.searchChannelData.append({freq:getRandomIntInclusive(9000 , 20000)})
    RadioGlobal.RadioData.searchChannelData.append({freq:getRandomIntInclusive(9000 , 20000)})
    RadioGlobal.RadioData.searchChannelData.append({freq:getRandomIntInclusive(9000 , 20000)})
    RadioGlobal.RadioData.searchChannelData.append({freq:getRandomIntInclusive(9000 , 20000)})
}


/**
    加载历史频道数据
**/
function loadHistoryChannel(){

    RadioGlobal.RadioData.historyChannelModel.clear()
    RadioGlobal.RadioData.historyChannelCurrentIndex = -1
    RadioGlobal.RadioData.historyChannelTotalIndex = -1
    /**
     * 加载 freqPageModel并获取 freqPageCurrentIndex（ /apps/data/audiodata.data ）
    **/
    var stFmCh = "fm_ch"
    var stCurrentIndex = "index"

    //根据“fm_ch”,反序列化ListModel数据
    jsonHelper.deserializeData(stFmCh)
    var dataList = jsonHelper.getFmData()
    for( var i = 0 ; i < dataList.length ; i++ )
    {
        RadioGlobal.RadioData.historyChannelModel.append( {"freq":dataList[i].freq} )
//        console.log( "freq: " , RadioGlobal.RadioData.historyChannelModel.get(i).freq)
    }

    //根据
    RadioGlobal.RadioData.historyChannelTotalIndex = RadioGlobal.RadioData.historyChannelModel.count
    console.log( "totalIndex: " , RadioGlobal.RadioData.historyChannelTotalIndex)

    //根据 index,反序列化当前索引数据
    jsonHelper.deserializeData(stCurrentIndex )
    RadioGlobal.RadioData.historyChannelCurrentIndex = jsonHelper.getCurrentIndex()
    console.log( "index: " , RadioGlobal.RadioData.historyChannelCurrentIndex)
}





